#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

#if defined(_MSC_VER) && defined(_DEBUG)
#include <crtdbg.h>
#include <conio.h>
#endif
#include <squirrel.h>
#include <sqstdblob.h>
#include <sqstdsystem.h>
#include <sqstdio.h>
#include <sqstdmath.h>	
#include <sqstdstring.h>
#include <sqstdaux.h>

#ifdef SQUNICODE
#define scfprintf fwprintf
#define scfopen	_wfopen
#define scvprintf vfwprintf
#else
#define scfprintf fprintf
#define scfopen	fopen
#define scvprintf vprintf
#endif

void errorfunc(HSQUIRRELVM v,const SQChar *s,...)
{
	va_list vl;
	va_start(vl, s);
	fprintf(stderr, s, vl);
	va_end(vl);
}

void printfunc(HSQUIRRELVM v, const SQChar *s, ...) 
        { 
                va_list arglist; 
                va_start(arglist, s); 
                vprintf(s, arglist); 
                va_end(arglist); 
        } 

int main(int argc, char* argv[])
{
	HSQUIRRELVM v;
	int x = 0;
	const SQChar *filename="test.nut";
	
	v=sq_open(1024);
	sq_setprintfunc(v,printfunc,NULL);

	sq_pushroottable(v);
	
	sqstd_register_bloblib(v);
	sqstd_register_iolib(v);
	sqstd_register_systemlib(v);
	sqstd_register_mathlib(v);
	sqstd_register_stringlib(v);
	
	//sq_pushstring(v,"xxx",-1);
	//sq_pushinteger(v,100);
	//sq_pushroottable(v);
	if(SQ_SUCCEEDED(sqstd_dofile(v,_SC("test.nut"),0,1))){
		printf("OK!\n");
	}
	sq_pushroottable(v);
	sq_pushstring(v,"s",-1);
	sq_get(v,-2);
	sq_getinteger(v,-1,&x);
	printf("sq_gettop:%d\n",sq_gettop(v));
	printf("%d\n",x);
	//aux library
	//sets error handlers
	sqstd_seterrorhandlers(v);
	sq_close(v);
	
	return 0;
}
